Java Package
A java package can be considered to a group of classes, interfaces or other sub-packages. Java supports two types of packages- built-in packages and user-defined packages. Java has a variety of built-in packages that allows you to use the features and functionality of stored classes, interfaces and methods.
Features of Java packages-
- Java packages allow easy categorization of classes and interfaces in order to handle many classes.
- It ensures access is protected.
- It resolves the problem of naming ambiguity.
Few Java packages-
Packages need to be imported to the current classes if you want to use particular class functionality from the packages.
Creating user-defined packages-
If you want to create a package, you need to used the package keyword before it.
package demo_pack;
public class Pack{
public static void main(String args[]){
System.out.println("First Package");
}
}
Once you create the package, you have to compile it. You can use the below command to compile the package.
Javac -d . Pack.java
In the above command,
-d
specifies the destination where to put the created class file.
If you want to run the package then you can run the below command.
Java demo_pack.Pack
You will get the output-
First Package
Accessing package from another package
Java supports different ways to access a package from another package, class.
- Using the import package.* statement.
- Using the import package.classname statement
- Using full name
Example using the package.*
Pack.java-
package demo_pack;
public class Pack{
public void m1(){
System.out.println("First Package"); }
public static void main(String args[]){
Pack p=new Pack();
p.m1();
}
}
Simple.java
package Simple_pack;
import demo_pack.*;
class Simple {
public static void main(String args[]){
Pack d= new Pack();
d.m1();
}
}
Output-
Run the commands in the same order as below to compile and run the files. First, compile the class then compile the package. You can access the class of other package using the package name only. (simple_pack.simple)- package_name.class_name.
Importing package.classname
Pack.java-
package demo_pack;
public class Pack{
public void m1(){
System.out.println("First Package"); }
public static void main(String args[]){
Pack p=new Pack();
p.m1();
}
}
Simple.java
package Simple_pack;
import demo_pack.Pack;
class Simple {
public static void main(String args[]){
Pack d= new Pack();
d.m1();
}
}
Output-
Using the full qualified name of the package-
Pack.java-
package demo_pack;
public class Pack{
public void m1(){
System.out.println("First Package"); }
public static void main(String args[]){
Pack p=new Pack();
p.m1();
}
}
Simple.java
package Simple_pack;
//import demo_pack.*;
class Simple {
public static void main(String args[]){
demo_pack.Pack d= new demo_pack.Pack();
d.m1();
}
}
Sub packages in Java-
You can create a package within another package.
package Simple_pack.demo_pack;//package within another package
class Simple {
public static void main(String args[]){
demo_pack.Pack d= new demo_pack.Pack();
d.m1();
}
}
output-